package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import groovy.time.BaseDuration;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeOaMeetAnnUsersEntity;
import org.edu.modules.oa.entity.VeOaMeetAnnounceEntity;
import org.edu.modules.oa.entity.VeOaMeetUsersEntity;
import org.edu.modules.oa.mapper.IVeOaMeetAnnUsersMapper;
import org.edu.modules.oa.mapper.IVeOaMeetAnnounceMapper;
import org.edu.modules.oa.mapper.IVeOaMeetUsersMapper;
import org.edu.modules.oa.service.IVeOaMeetAnnounceService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;

@Service
public class IVeOaMeetAnnounceServiceImpl implements IVeOaMeetAnnounceService {
    @Resource
    private IVeOaMeetAnnounceMapper veOaMeetAnnounceMapper;
    @Resource
    private IVeOaMeetUsersMapper veOaMeetUsersMapper;
    @Resource
    private IVeOaMeetAnnUsersMapper veOaMeetAnnUsersMapper;
    //初始化加载通知表格
    public HashMap<String, Object> loadAnnounceTable(Integer start, Integer pageSize, String title,Integer meetId) {
        HashMap<String,Object> map = new HashMap<>();
        QueryWrapper<VeOaMeetAnnounceEntity> wrapper = new QueryWrapper();
        IPage<VeOaMeetAnnounceEntity> page = new Page<>(start,pageSize);
        if(null != title && !"".equals(title)){
            wrapper.like("title",title);
        }
        wrapper.eq("meet_id",meetId);
        wrapper.orderByDesc("create_time");
        page = veOaMeetAnnounceMapper.selectPage(page,wrapper);
        List<VeOaMeetAnnounceEntity> list = page.getRecords();
        int count = veOaMeetAnnounceMapper.selectCount(wrapper);
        map.put("list",list);
        map.put("count",count);
        return map;
    }

    //删除一条通知的方法
    public int deleteAnnounceById(Integer id) {
        int res = veOaMeetAnnounceMapper.deleteById(id);
        return res;
    }

    //查看参加当前会议的全部用户ve_oa_meet_users
    public List<VeOaMeetUsersEntity> selectMeetUsersById(Integer meetId) {
        QueryWrapper<VeOaMeetUsersEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("meet_id",meetId);
        List<VeOaMeetUsersEntity> list = veOaMeetUsersMapper.selectList(wrapper);
        return list;
    }

//    select * From  meet where meet_id=1

//    //用id查看参会人员名称
//    public VeOaMeetUsersEntity selectUserName(Integer meetId, Integer userId) {
//        QueryWrapper<VeOaMeetUsersEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq("meet_id", meetId);
//        wrapper.eq("user_id", userId);
//        VeOaMeetUsersEntity veOaMeetUsersEntity = veOaMeetUsersMapper.selectOne(wrapper);
//        return veOaMeetUsersEntity;
//    }

    //用会议id查看通知的参会人员----通知人员表ve_oa_meet_ann_users
    public List<VeOaMeetAnnUsersEntity> selectAnnUsersByMeetId(Integer meetId,Integer anId) {
        QueryWrapper<VeOaMeetAnnUsersEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("meet_id",meetId);
        wrapper.eq("an_id",anId);
        List<VeOaMeetAnnUsersEntity> list = veOaMeetAnnUsersMapper.selectList(wrapper);
        return list;
    }

   //添加通知表和通知人员表
   @Transactional(rollbackOn = Exception.class)
    public int insertMeetUserAnno(VeOaMeetAnnounceEntity veOaMeetAnnounceEntity,String ids) {
        VeOaMeetAnnUsersEntity veOaMeetAnnUsersEntity = new VeOaMeetAnnUsersEntity();
        //先添加会议
        int res = veOaMeetAnnounceMapper.insert(veOaMeetAnnounceEntity);
        if(res == 0){
            return 0;
        }
        //会议id
        veOaMeetAnnUsersEntity.setMeetId(veOaMeetAnnounceEntity.getMeetId());
        //通知表的主键id
        veOaMeetAnnUsersEntity.setAnId(veOaMeetAnnounceEntity.getId());
        if(null != ids && !ids.equals("")){
            String[] arr = ids.split(",");
            for (String s : arr) {
                if(null != s && !s.equals("") && !s.equals(",")){
                    veOaMeetAnnUsersEntity.setUserId(s);
                    //获取用户名称
                    QueryWrapper<VeOaMeetUsersEntity> wrapper = new QueryWrapper<>();
                    wrapper.eq("meet_id", veOaMeetAnnUsersEntity.getMeetId());
                    wrapper.eq("user_id", s);
                    VeOaMeetUsersEntity veOaMeetUsersEntity = veOaMeetUsersMapper.selectOne(wrapper);
                    //添加用户名称
                    veOaMeetAnnUsersEntity.setUserName(veOaMeetUsersEntity.getUserName());
                    veOaMeetAnnUsersEntity.setTerminalId(1);
                    veOaMeetAnnUsersMapper.insert(veOaMeetAnnUsersEntity);
                }
            }
        }
        return res;
    }

    //修改会议通知的方法
    @Transactional(rollbackOn = Exception.class)
    public int updateMeetUserAnno(VeOaMeetAnnounceEntity veOaMeetAnnounceEntity, String ids) {
        VeOaMeetAnnUsersEntity veOaMeetAnnUsersEntity = new VeOaMeetAnnUsersEntity();
        int res = veOaMeetAnnounceMapper.updateById(veOaMeetAnnounceEntity);
        if(res == 0){
            return 0;
        }
        QueryWrapper<VeOaMeetAnnUsersEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("meet_id",veOaMeetAnnounceEntity.getMeetId());
        wrapper.eq("an_id",veOaMeetAnnounceEntity.getId());
        veOaMeetAnnUsersEntity.setMeetId(veOaMeetAnnounceEntity.getMeetId());
        //先把已通知的人员删掉
        veOaMeetAnnUsersMapper.delete(wrapper);
        String [] arr = ids.split(",");
        for (String s : arr) {
            if(null != s && !"".equals(s)){
                veOaMeetAnnUsersEntity.setUserId(s);
                //获取用户名称
                QueryWrapper<VeOaMeetUsersEntity> w = new QueryWrapper<>();
                w.eq("meet_id", veOaMeetAnnUsersEntity.getMeetId());
                w.eq("user_id", s);
                VeOaMeetUsersEntity veOaMeetUsersEntity = veOaMeetUsersMapper.selectOne(w);
                veOaMeetAnnUsersEntity.setUserName(veOaMeetUsersEntity.getUserName());
                veOaMeetAnnUsersEntity.setAnId(veOaMeetAnnounceEntity.getId());
                //添加用户
                veOaMeetAnnUsersEntity.setTerminalId(1);
                veOaMeetAnnUsersMapper.insert(veOaMeetAnnUsersEntity);
            }
        }
        return res;
    }







}
